<?php

namespace App\Providers;

use App\Admin;
use App\DoctorExpert;
use App\DoctorType;
use App\ShopInfo;
use App\Vip;
use Illuminate\Support\ServiceProvider;
use Validator;
use Hash;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // 验证手机号格式是否正确
        Validator::extend('mobile', function ($attribute, $value, $parameters) {
            return mobile($value);
        });

        //验证是否是数字
        Validator::extend('is_number', function ($attribute, $value, $parameters) {
            return is_numeric($value);
        });

        //验证是否是圈主
        Validator::extend('is_admin', function ($attribute, $value, $parameters) {
            return Admin::where('name', $value)->exists();
        });

        //校验密码是否正确
        Validator::extend('check_password', function ($attribute, $value, $parameters) {
            return Hash::check($value, Admin::where('name', request('name'))->value('password'));
        });


        //医生擅长领域名称是否重复
        Validator::extend('has_doctor_expert_same', function ($attribute, $value, $parameters) {
            return !DoctorExpert::where('title', $value)->exists();
        });
        //医生擅长领域排序是否重复
        Validator::extend('has_doctor_expert_display_same', function ($attribute, $value, $parameters) {
            return !DoctorExpert::where('display', $value)->exists();
        });

        //医生类型名称是否重复
        Validator::extend('has_doctor_type_same', function ($attribute, $value, $parameters) {
            return !DoctorType::where('title', $value)->exists();
        });
        //医生类型排序是否重复
        Validator::extend('has_doctor_type_display_same', function ($attribute, $value, $parameters) {
            return !DoctorType::where('display', $value)->exists();
        });


        //是否有这个类型
        Validator::extend('is_consulttype', function ($attribute, $value, $parameters) {
            return key_exists($value, ShopInfo::$DisplayConsultType);
        });
        //是否有这个VIP级别
        Validator::extend('is_vip', function ($attribute, $value, $parameters) {
            if (!$value) {
                return true;
            }
            return Vip::where('id', $value)->exists();
        });

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
